$.ready(function () {
    $("#entry_date").datepicker({ dateFormat: "yy-mm-dd" });
    $('input[name="salary_type"]').click(function(item){
        var t_type = $('input[name="transfer_type"]:checked').val();
        if($(this).attr('value')==5){
            $('#a_apply_job').html('*');
        }else{
            if(typeof t_type =='undefined'){
                $('#a_apply_job').html('&nbsp;&nbsp;');
            }
        }
    });
    $('input[name="transfer_type"]').click(function(item){
        $('#a_apply_job').html('*');
    });

    autoComplete();
    autoComplete1();
    autoComplete2();
    autoComplete3();
    $("#submit").bind('click',function(){
        var type = $('input[name="salary_type"]:checked').val();
        var job_nm = $('#input_type').val();
        var t_type=$('input[name="transfer_type"]:checked').val();
        if((type == 5 && job_nm=='') || (t_type && job_nm=='')){
            alert('申请职位不能为空!');
        }else if(($("#ipt_name").val() && $("#ipt_code").val()) && $("#entry_date").val() && $("#input_type_user").val()){
            $('#form1').submit();
        }else{
            alert('表单填写不完整!');
        }
    });

}());

function autoComplete(){
    $("#ipt_name").autocomplete({
        source: function(request, response) {
            $.ajax({
                url:url_user,
                dataType: "json",
                data: {
                    name: request.term
                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                        return {
                            label:  item.name,
                            value:  item.user_name,
                            job_name: item.position_name,
                            user_id:  item.user_id,
                            job_id :item.job_id,
                            user_code:item.user_code,
                            department_name:item.department_name,
                            m_level:item.m_level,
                            p_level:item.p_level,
                            city_id:item.city_id
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            var items = ui.item;

            var user_id = items.user_id,
                input1 = $('input[type="hidden"]')[0];
            var city_id =items.city_id,
                input2 = $('input[type="hidden"]')[2];
            input1.value = user_id;
            input2.value = city_id;

            var user_code = items.user_code,        //工号
                job_name = items.job_name,          //职位
                p_level = items.p_level,         //职级p
                m_level = items.m_level,         //职级m
                dep_name = items.department_name;   //部门

            var ipt1 = $('#ipt_code'),
                ipt2 = $('#ipt_job'),
                ipt3 = $('#ipt_level'),
                ipt4 = $('#ipt_department');

            ipt1.val(user_code);
            ipt2.val(job_name);
            ipt3.val(p_level + m_level);
            ipt4.val(dep_name);

            positionResult = true;
        }
    });

    $("#ipt_name").bind("keydown",function(event){
        if((event.keyCode == 13 && positionResult == true)||(event.keyCode == 9 && positionResult == true) || (event.keyCode == 32 && positionResult == true)){
            return;
        }
        positionResult = false;
    });

    $("#ipt_name").bind("blur",function(){
        if(!positionResult){
            $(".leader_id").val("");
            $(this).val("");

        }
    });

    jobResult = false;
    positionResult = false;
}

function autoComplete1(){
    $("#ipt_code").autocomplete({
        source: function(request, response) {
            $.ajax({
                url:url_user,
                dataType: "json",
                data: {
                    name: request.term
                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                        return {
                            label:  item.code_name,
                            value:  item.user_code,
                            job_name: item.position_name,
                            user_id:  item.user_id,
                            job_id :item.job_id,
                            user_name:item.user_name,
                            department_name:item.department_name,
                            m_level:item.m_level,
                            p_level:item.p_level
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            var items = ui.item;

            var user_id = items.user_id,
                input1 = $('input[type="hidden"]')[0];
            var city_id = items.city_id,
                input2 = $('input[type="hidden"]')[2];
            input1.value = user_id;
            input2.value = city_id;

            var user_name = items.user_name,        //姓名
                job_name = items.job_name,          //职位
                p_level = items.p_level,            //职级p
                m_level = items.m_level,            //职级m
                dep_name = items.department_name;   //部门

            var ipt0 = $('#ipt_name'),
                ipt2 = $('#ipt_job'),
                ipt3 = $('#ipt_level'),
                ipt4 = $('#ipt_department');

            ipt0.val(user_name);
            ipt2.val(job_name);
            ipt3.val(p_level + m_level);
            ipt4.val(dep_name);

            positionResult = true;
        }
    });

    $("#ipt_code").bind("keydown",function(event){
        if((event.keyCode == 13 && positionResult == true)||(event.keyCode == 9 && positionResult == true) || (event.keyCode == 32 && positionResult == true)){
            return;
        }
        positionResult = false;
    });
    $("#ipt_code").bind("blur",function(){
        if(!positionResult){
            $(".leader_id").val("");
            $(".leader_job").val("");
            $(this).val("");
        }
    });


}
userResult = false;
positionResult = false;
//搜索职位
function autoComplete2(){
    $('#input_type').autocomplete({
        source: function(request, response) {
            $.ajax({
                url: ajaxUrl2,
                dataType: "json",
                data: {
                    name: request.term
                },
                success: function(data) {
                    flag = 1;
                    response( $.map( data.data, function( item ) {
                        return {
                            label: item.job_name,
                            value: item.job_name,
                            job_id: item.job_id,
                            city_id:item.city_id
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            var id = ui.item.job_id;
            var city_id = ui.item.city_id;
            $("#new_city_id").val(city_id);
            $("input[name='job_id']").val(id);
            positionResult = true;
        }
    })

    $("#input_type").bind("keydown",function(event){
        if((event.keyCode == 13 && positionResult == true)||(event.keyCode == 9 && positionResult == true) || (event.keyCode == 32 && positionResult == true)){
            return;
        }
        positionResult = false;
    });

    $("#input_type").bind("blur",function(){
        if(!positionResult){
            $(this).val("");
            $("#new_city_id").val('');
            $("input[name='job_id']").val('');

        }
    });
}

//搜索主管
function autoComplete3(){
    $("#input_type_user").autocomplete({
        source: function(request, response) {
            $.ajax({
                url:url_user,
                dataType: "json",
                data: {
                    name: request.term
                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                        return {
                            label:  item.name,
                            value:  item.name,
                            job_name: item.position_name,
                            user_id:  item.user_id,
                            job_id :item.job_id,
                            user_code:item.user_code,
                            department_name:item.department_name,
                            m_level:item.m_level,
                            p_level:item.p_level,
                            city_id:item.city_id
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            var items = ui.item;
            $('#super_user_id').val(items.user_id);
            userResult = true;
        }
    });

    $("#input_type_user").bind("keydown",function(event){
        if((event.keyCode == 13 && userResult == true)||(event.keyCode == 9 && userResult == true) || (event.keyCode == 32 && userResult == true)){
            return;
        }
        userResult = false;
    });

    $("#input_type_user").bind("blur",function(){
        if(!userResult){
            $('#super_user_id').val('');
            $(this).val("");

        }
    });
}
var flag = 0;